package com.example.demo.util;

import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


/**
 * @author shuxiangcheng
 * @Description 字符串工具类
 */
public class StringUtilsEx extends StringUtils {
	
	private static final char SEPARATOR = '_';
	public static final String BRIDGE = "-";
	public static final String EMPTY_STRING = "";
	public static final String DEFAULT_SHARDING_TAG = "default";
	/**
	 * 默认分隔符“|”
	 */
	public static final String DEFALUT_SPLIT_CHAR = "\\|";
	/**
	 * 默认分隔符“,”
	 */
	public static final String DEFALUT_SPLIT_CHAR_COMMA = ",";
	
	/**
	 * ASCII 字符 Unicode 值
	 */
	private static final String REGEX_ASCII_CHARACTHERS = "[\\u0000-\\u0080]*";

	/**
	 * 给定字符串是否只包含ASCII字符
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isASCIIOnly(String str) {
		if (isEmpty(str)) {
			return true;
		}
		return str.matches(REGEX_ASCII_CHARACTHERS);
	}
	

	/**
	 * 是否为空值
	 * 
	 * @param value
	 * @return true 表示为空
	 * @author gavinlong
	 */
	public static boolean isEmpty(String value) {
		return null == value || "".equals(value);
	}
	
	
	/**
	 * 分割字符串，并转成List
	 * @param value
	 * @param charSplit
	 * @return
	 * @author gavinlong
	 */
	public static List<Long> splitToLongList(String value,String charSplit) {
		return Arrays.asList(value.split(charSplit)).stream().filter(item -> !StringUtilsEx.isEmpty(item))
				.map(item->{return Long.valueOf(item);}).collect(Collectors.toList());
	}

	/**
	 * 分隔集合中的字符串
	 * 
	 * @param values
	 *            数据集合 {@link List}
	 * @param charSplit
	 *            分隔字符
	 * @return
	 * @author gavinlong
	 */
	public static List<String> split(List<String> values, String charSplit) {
		return values.stream().map(item -> item.split(charSplit)).flatMap(Arrays::stream).collect(Collectors.toList());
	}

	/**
	 * 分隔字符串
	 * 
	 * @param value
	 *            字符数据
	 * @param charSplit
	 *            分隔字符
	 * @return
	 * @author gavinlong
	 */
	public static List<String> splitToList(String value, String charSplit) {
		return Arrays.asList(value.split(charSplit));
	}

	/**
	 * 
	 * @param list
	 * @return
	 * @author gavinlong
	 */
	public static boolean isNullOrEmpty(Collection<?> list) {
		return null == list || list.isEmpty();
	}
	
	/**
	 * 
	 * @param Object
	 * @return
	 * @author gavinlong
	 */
	public static boolean isNull(Object obj) {
		return null == obj;
	}

	/**
	 * 符合11-15位长度的数据字符窜
	 * 
	 * @param value
	 * @return
	 * @author gavinlong
	 */
	public static boolean checkNumber(String value) {
		Pattern pt = Pattern.compile("^\\d{11,15}$");
		Matcher matcher = pt.matcher(value);
		return matcher.find();
	}
	
	/**
	 * 验证重发策略
	 * @param value
	 * @return
	 */
	public static boolean checkResendNumber(String value) {
		Pattern pt = Pattern.compile("^[01]$");
		Matcher matcher = pt.matcher(value);
		return matcher.find();
	}
	
	/**
	 * 验证通道签名扩展码
	 * @param value
	 * @return
	 */
	public static boolean checkSpExtendCode(String value) {
		Pattern pt = Pattern.compile("^\\d{1,21}$");
		Matcher matcher = pt.matcher(value);
		return matcher.find();
	}

	/**
	 * 是否包含字符串
	 * 
	 * @param str
	 *            验证字符串
	 * @param strs
	 *            字符串组
	 * @return 包含返回true
	 */
	public static boolean inString(String str, String... strs) {
		if (str != null && strs != null) {
			for (String s : strs) {
				if (str.equals(trim(s))) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 是否包含字符串
	 * 
	 * @param str
	 *            验证字符串
	 * @param strs
	 *            字符串组
	 * @return 包含返回true
	 */
	public static boolean inStringIgnoreCase(String str, String... strs) {
		if (str != null && strs != null) {
			for (String s : strs) {
				if (str.equalsIgnoreCase(trim(s))) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 首字母大写
	 */
	public static String cap(String str) {
		return capitalize(str);
	}

	/**
	 * 首字母小写
	 */
	public static String uncap(String str) {
		return uncapitalize(str);
	}

	/**
	 * 驼峰命名法工具
	 * 
	 * @return camelCase("hello_world") == "helloWorld" capCamelCase("hello_world")
	 *         == "HelloWorld" uncamelCase("helloWorld") = "hello_world"
	 */
	public static String camelCase(String s) {
		if (s == null) {
			return null;
		}

		s = s.toLowerCase();

		StringBuilder sb = new StringBuilder(s.length());
		boolean upperCase = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);

			if (c == SEPARATOR) {
				upperCase = true;
			} else if (upperCase) {
				sb.append(Character.toUpperCase(c));
				upperCase = false;
			} else {
				sb.append(c);
			}
		}

		return sb.toString();
	}

	/**
	 * 驼峰命名法工具
	 * 
	 * @return camelCase("hello_world") == "helloWorld" capCamelCase("hello_world")
	 *         == "HelloWorld" uncamelCase("helloWorld") = "hello_world"
	 */
	public static String capCamelCase(String s) {
		if (s == null) {
			return null;
		}
		s = camelCase(s);
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}

	/**
	 * 驼峰命名法工具
	 * 
	 * @return camelCase("hello_world") == "helloWorld" capCamelCase("hello_world")
	 *         == "HelloWorld" uncamelCase("helloWorld") = "hello_world"
	 */
	public static String uncamelCase(String s) {
		if (s == null) {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		boolean upperCase = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);

			boolean nextUpperCase = true;

			if (i < (s.length() - 1)) {
				nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
			}

			if ((i > 0) && Character.isUpperCase(c)) {
				if (!upperCase || !nextUpperCase) {
					sb.append(SEPARATOR);
				}
				upperCase = true;
			} else {
				upperCase = false;
			}

			sb.append(Character.toLowerCase(c));
		}

		return sb.toString();
	}
	
	/**
	 * 替换掉HTML标签方法
	 */
	public static String stripHtml(String html) {
		if (isBlank(html)){
			return "";
		}
		//html.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "");
		String regEx = "<.+?>";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(html);
		String s = m.replaceAll("");
		return s;
	}
	
	public static String longToString(Long value) {
		if(StringUtilsEx.isNull(value))
			return StringUtilsEx.EMPTY;
		return value.toString();
	}

}
